package cn.lastjune.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import cn.lastjune.util.ExcelUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.lastjune.dao.impl.AreaDaoImpl;

@Controller
@RequestMapping("/excel")

public class ExcelBuild {
	@Autowired
	AreaDaoImpl dao;
	
	@RequestMapping(method = RequestMethod.POST)
	public void excel(HttpServletRequest request, HttpServletResponse response)
			throws IOException, SQLException {
		List<Object[]> list = dao.getRate();
		int count = list.size();
		List datas = new ArrayList();
		//对数据进行组装
		for (Object[] o : list) {
			String[] data = { o[0].toString(), o[1].toString() };
			if(data!=null){
				datas.add(data);
			}
		}
		// 写入excel
		try{
			//excel文件写入流定义
			OutputStream out2 = new FileOutputStream(new File(
					"c:/rate.xls"));
			//调用该工具类的静态方法，完成数据导出到Excel的操作
			ExcelUtil.writeExcel(out2, datas, new String[]{"地区","频率"});
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
